package com.hemerasolutions.v2trainer.component.table;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.event.ActionEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import com.hemerasolutions.v2trainer.component.BaseComponent;
import com.hemerasolutions.v2trainer.component.interfaces.detail.ExerciseDetailsCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.table.ExerciseTableCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.table.GroupedExerciseTableCompInterface;
import com.hemerasolutions.v2trainer.delegate.interfaces.ExerciseTableDelegateInterface;
import com.hemerasolutions.v2trainer.model.exercise.Exercise;
import com.hemerasolutions.v2trainer.model.exercise.VersionableExercise;
import com.hemerasolutions.v2trainer.model.exerciseGroup.ExerciseGroup;


@Controller
@Component("groupedExerciseTableComp")
@Scope("prototype")
public class GroupedExerciseTableComp extends BaseComponent implements GroupedExerciseTableCompInterface,ExerciseTableDelegateInterface {

	private static final long serialVersionUID = 1L;
	
	private ExerciseGroup exerciseGroup;

	private Exercise exercise;	
	
	@Autowired
	private ExerciseTableCompInterface exerciseTable;
	
	@Autowired
	private ExerciseDetailsCompInterface exerciseDetails;

	
	public GroupedExerciseTableComp(){
		super();
	}
	

	@PostConstruct
	public void init(){
		super.init();
	}
		
	public void refresh(){
		this.exerciseTable.setModality(this.exerciseGroup.getModality());
		this.exerciseTable.setDelegate(this);
		this.exerciseTable.refresh();
	}
	
	@Override
	public void toViewExercise(){
		this.exerciseDetails.setVersionableExercise(exercise.getVersionableExercise());
		this.exerciseDetails.setExercise(exercise);
		this.exerciseDetails.refresh();
	}

	@Override
	public void upExercise(ActionEvent ev){
		this.exerciseGroup.upExercise((Exercise)ev.getComponent().getAttributes().get("exercise"));
	}
	
	@Override
	public void downExercise(ActionEvent ev){
		this.exerciseGroup.downExercise((Exercise)ev.getComponent().getAttributes().get("exercise"));
	}
	
	
	@Override
	public void deleteExercise(){
		this.exerciseGroup.getExercises().remove(this.exercise);
	}
		
	
	@Override
	public boolean onVersionableExerciseSelected(VersionableExercise versionableExercise) {

		if(this.exerciseGroup.getExercises().contains(versionableExercise.getExercise())){
			this.showErrorMessage(this.localizedText("Error"), this.localizedText("ExerciseDuplicated"));
		}else{
			this.exerciseGroup.getExercises().add(versionableExercise.getExercise());
		}
		return false;
	}


	@Override
	public List<VersionableExercise> onFilterExercises(List<VersionableExercise> filteredExercises) {

		for(Exercise ex:this.exerciseGroup.getExercises()){
			filteredExercises.remove(ex.getVersionableExercise());	
		}
		
		return filteredExercises;
	}


	@Override
	public ExerciseGroup getExerciseGroup() {
		return exerciseGroup;
	}


	@Override
	public void setExerciseGroup(ExerciseGroup exerciseGroup) {
		this.exerciseGroup = exerciseGroup;
	}


	@Override
	public Exercise getExercise() {
		return exercise;
	}


	@Override
	public void setExercise(Exercise exercise) {
		this.exercise = exercise;
	}


	@Override
	public ExerciseTableCompInterface getExerciseTable() {
		return exerciseTable;
	}


	@Override
	public void setExerciseTable(ExerciseTableCompInterface exerciseTable) {
		this.exerciseTable = exerciseTable;
	}


	@Override
	public ExerciseDetailsCompInterface getExerciseDetails() {
		return exerciseDetails;
	}


	@Override
	public void setExerciseDetails(
			ExerciseDetailsCompInterface exerciseDetailsComp) {
		this.exerciseDetails = exerciseDetailsComp;
	}
	
	
	
	
}
